/*
 * File: vfsdriver.c
 *
 * Description: This is a driver program for testing your VFS system using an interaction script as input
 * You need to make additional calls to your respective functions as noted in the comments below
 * Make sure the output you display is exactly as per the given specifications for you. Do NOT print
 * any extra output (like debug messages) in the final version of your driver program. You can use this driver program
 * in a in incremental manner as you keep implementing one operator after another. For operators not yet implemented,
 * you can leave the output as given ("TO_BE_DONE"). So you can use this program as your "main" program for testing purposes.
 *
 * DO NOT write the full code for operators in the driver program! You must only CALL your functions from here.
 *
 * Usage: vfsdriver <scriptfilename>
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../include/test.h"
#include "../include/bst.h"
#include "../include/vfs_errorcodes.h"
#define BUFSIZE 200
#define CMDSIZE 30
#define PARSIZE 100


void processcommand( char *command, char *P1, char *P2, char *P3 );
 FILE *result;
  int mount_flag=0;
int main( int argc, char *argv[] )
{
	FILE *scriptfp;
       
	char linebuffer[BUFSIZE];
	char command[CMDSIZE], par1[PARSIZE], par2[PARSIZE], par3[PARSIZE];
	char *token;

	if( argc != 2 ){
		fprintf(stderr,"Usage: vfsdriver <scriptfile>\n");
		return(1);
	}

	if( (scriptfp=fopen(argv[1],"r")) == NULL ){
		fprintf(stderr,"Unable to open script file: %s\n", argv[1]);
		return(2);
	}


     
	while( fgets(linebuffer, sizeof(linebuffer), scriptfp) != NULL ){
		/* This output is for debugging... do not uncomment in final version */
		/*
		printf("==================================================\n");
		printf("Processing: %s", linebuffer);
		printf("==================================================\n");
		*/

		/* Remove the extra newline character in the end of line */
		linebuffer[ strlen(linebuffer)-1 ] = '\0';
		
		/* Get the command and the parameters using tokenizer */
		strcpy( command, (token = strtok(linebuffer, " ")) == NULL ? "" : token );
		
		strcpy( par1, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par2, (token = strtok(NULL, " ")) == NULL ? "" : token );
		strcpy( par3, (token = strtok(NULL, " ")) == NULL ? "" : token );
		/* printf("Command:%s:p1:%s:p2:%s:p3:%s\n",command, par1, par2, par3); */

		processcommand( command, par1, par2, par3 );
	}
}

void processcommand( char *command, char *P1, char *P2, char *P3 )
{
	
        if(strlen(P1)>1 && P1[strlen(P1)-1]=='/')  
           P1[strlen(P1)-1]='\0';

        if(strlen(P2)>1 && P2[strlen(P2)-1]=='/')  
           P2[strlen(P2)-1]='\0';

        if(strlen(P3)>1 && P3[strlen(P3)-1]=='/')  
           P3[strlen(P3)-1]='\0';
         
        if( strcmp(command, "createvfs") == 0 ){
                
                if(strlen(P1)==0 || strlen(P2)==0)
		{
                   printf("createvfs_FAILURE ");
		   printf(ERR_VFS_CREATE_00);
		   printf("\n");
		   return;
		}
                
		int size = atoi(P2);
                
		printf("%s\n",create_file_system (P1,size));

	}
	else if( strcmp(command, "mountvfs") == 0 ) {
			             

			   
		  if(strlen(P1)==0 || P1==NULL)
		{
		  printf("mountvfs_FAILURE ");
                  printf(ERR_VFS_MOUNT_00);
		  printf("\n");
		  return;	
		}   
                if(mount_flag==1)
		{
	           printf("mountvfs_FAILURE ");
                   printf(ERR_VFS_MOUNT_03);
		   printf("\n");
		  return; 
			
		}	

                char * output=mount(P1);  
		if(strcmp(output,"mountvfs_SUCCESS")==0)
			mount_flag=1;	
                     printf("%s\n",output);
		}
	else if( strcmp(command, "unmountvfs") == 0 ) {

		  if(P1==NULL || strlen(P1)==0)
		{
                  printf("unmountvfs_FAILURE ");
                  printf(ERR_VFS_UNMOUNT_00);
		  printf("\n");
		  return;	
		}   

		if(mount_flag==0)
		{
			printf("unmountvfs_FAILURE ");			
			printf(ERR_VFS_UNMOUNT_03);
			printf("\n");
			return;
		}
		 char * output=unmount(P1); 
		

		if(strcmp(output,"unmountvfs_SUCCESS")==0)
                        mount_flag=0;
			printf("%s\n",output);
	}
		
	else if( strcmp(command, "makedir") == 0 )
	{
		 if(strlen(P1)==0 ||  strlen(P2)==0)
		{
                  printf("makedir_FAILURE ");
	          printf(ERR_VFS_MAKEDIR_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("unmountvfs_FAILURE ");			
			printf(ERR_VFS_MAKEDIR_05);
			printf("\n");
			return;
		} 		
		
		printf("%s\n",create_rec_dir (P1,P2));
	}
	else if( strcmp(command, "deletedir") == 0 )
        {
		
		 if(strlen(P1)==0)
		{
                  printf("deletedir_FAILURE ");
		  printf(ERR_VFS_DELETEDIR_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("deletedir_FAILURE ");			
			printf(ERR_VFS_DELETEDIR_04);
			printf("\n");
			return;
		} 
		printf("%s\n",removedir (P1,'d'));

	}
	else if( strcmp(command, "movedir") == 0 )
	{
		 if(strlen(P1)==0 ||strlen(P2)==0)
		{
                  printf("movedir_FAILURE ");
		  printf(ERR_VFS_MOVEDIR_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("movedir_FAILURE ");			
			printf(ERR_VFS_MOVEDIR_08);
			printf("\n");
			return;
		} 		
		
		printf("%s\n",move_dir (P1,P2,'d'));

	}
	else if( strcmp(command, "listdir") == 0 ){
		 if(strlen(P1)==0 || strlen(P2)==0 || strlen(P3)==0)
		{
                  printf("listdir_FAILURE ");  
		  printf(ERR_VFS_LISTDIR_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("listdir_FAILURE ");
			printf(ERR_VFS_LISTDIR_03);
			printf("\n");
			return;
		} 
               
		int flag = atoi(P2);
		printf("%s\n",list (P1,flag,P3));	
	}
	else if( strcmp(command, "addfile") == 0 )
	{
		if(strlen(P1)==0 || strlen(P2)==0 || strlen(P3)==0)
		{
                  printf("addfile_FAILURE ");
		  printf(ERR_VFS_ADDFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("addfile_FAILURE ");			
			printf(ERR_VFS_ADDFILE_07);
			printf("\n");
			return;
		} 	

		printf("%s\n",create (P1,P2,P3));

	}
	else if( strcmp(command, "listfile") == 0 )
	{
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("listfile_FAILURE ");
		  printf(ERR_VFS_LISTFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			
			printf("listfile_FAILURE ");
			printf(ERR_VFS_LISTFILE_04);
			printf("\n");
			return;
		}		
		printf("%s\n",list_File (P1,P2,'l'));

	}
	else if( strcmp(command, "updatefile") == 0 )
	{
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("updatefile_FAILURE ");
		  printf(ERR_VFS_UPDATEFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("updatefile_FAILURE ");			
			printf(ERR_VFS_UPDATEFILE_04);
			printf("\n");
			return;
		}		

		printf("%s\n",update_file_contents (P1,P2));
	}

	else if( strcmp(command, "removefile") == 0 )
	{
		if(strlen(P1)==0)
		{
                  printf("removefile_FAILURE ");
		  printf(ERR_VFS_REMOVEFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			
			printf("removefile_FAILURE ");
			printf(ERR_VFS_REMOVEFILE_02);
			printf("\n");
			return;
		}		
		printf("%s\n",removedir (P1,'f'));

	}
	else if( strcmp(command, "movefile") == 0 )
	{
		
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("movefile_FAILURE ");
		  printf(ERR_VFS_MOVEFILE_00);
		  printf("\n"); vfsproject.iiitb@gmail.com
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("movefile_FAILURE ");			
			printf(ERR_VFS_MOVEFILE_06);
			printf("\n");
			return;
		}
		printf("%s\n",movefile (P1,P2));


	}
	else if( strcmp(command, "copyfile") == 0 )
	{
	
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("copyfile_FAILURE ");
	          printf(ERR_VFS_MOVEFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("copyfile_FAILURE ");			
			printf(ERR_VFS_MOVEFILE_06);
			printf("\n");
			return;
		}
		printf("%s\n",copy (P1,P2));

	}
	else if( strcmp(command, "exportfile") == 0 )
	{
	
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("exportfile_FAILURE ");
		  printf(ERR_VFS_MOVEFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("exportfile_FAILURE ");			
			printf(ERR_VFS_MOVEFILE_06);
			printf("\n");
			return;
		}
		printf("%s\n",list_File(P1,P2,'e'));

	}
	else if( strcmp(command, "searchfile") == 0 )
	{
		if(strlen(P1)==0 || strlen(P2)==0)
		{
                  printf("searchfile_FAILURE ");
		  printf(ERR_VFS_MOVEFILE_00);
		  printf("\n");
		  return;	
		} 
		if(mount_flag==0)
		{
			printf("searchfile_FAILURE ");			
			printf(ERR_VFS_MOVEFILE_06);
			printf("\n");
			return;
		}		

		printf("%s\n",search_with_filename (P1,P2));

	}
	else
		printf("Ignoring invalid command %s\n", command);
}



